﻿var express = require('express');
var router = express.Router();
var filter=require('../util/filter');
var util=require('util');
var shopDAO=require('../dao/shopDao');
var petDAO=require('../dao/petDao');
var orderDAO=require('../dao/orderDao');
var personal=require('../dao/w_personnal');
var fs=require('fs');
var formidable=require('formidable');

/* GET users listing. */

router.get('/', function(req, res) {
    if(req.session.userID==undefined){
        req.session.urldate='/users';
        res.redirect('/login');
    }else {
        personal.pic(req.session.usertel,function(result){

            if(result.length==0){
                res.render('personnal',{username:req.session.userID,res:0});

            }else{


                console.log('___________ceshi_____'+result[0].useraddress);

                res.render('personnal',{res:result,username:req.session.userID,pic:result[0].pic});

            }
        });
    }
});

router.get('/foster',function(req, res) {
        req.session.urldate='/users/foster';
        shopDAO.get_shop(function (result) {
            console.log('shop?>>>>>>>>>>>' + JSON.stringify(result));
            res.render('w_Pet_foster', {shops: result, user: req.session.userID,username:req.session.userID});
            console.log("shop11>>>>>>>>>" + req.session.userID);
            console.log("shop33>>>>>>>>>" + JSON.stringify(result));
        })
});



router.get('/editor-basic',function(req, res) {
    if(req.session.userID==undefined){
        req.session.urldate='/users/editor-basic';
        res.redirect('/login');
    }else {
        req.session.urldate='/users';
        personal.pic(req.session.usertel, function (result) {
            function FormatDate (strTime) {
                var date = new Date(strTime);
                var month;
                var day;
                if(parseInt(date.getMonth())<9){
                    month="0"+parseInt(date.getMonth()+1)
                }else{
                    month=date.getMonth();
                }
                if(parseInt(date.getDate())<10){
                    day="0"+date.getDate()
                }else{
                    day=date.getDate();
                }

                return date.getFullYear()+"-"+month+"-"+day;
            }

            if (result.length == 0) {
                console.log(JSON.stringify(result));
                res.render('editor-basic', {res:result,username:req.session.userID});
            } else {
                console.log('测试数据库里的日期'+result[0].userbirth);
                var final_date=FormatDate(result[0].userbirth);
                console.log("FFFFFFFFFFFFF" + final_date);
                console.log(JSON.stringify(result));
                res.render('editor-basic', {res:result,date:final_date, username: req.session.userID});

            }
        })
    }
});
router.post('/editor-basic',function(req,res){
    var city=req.body.city;
    personal.update_user(req.body.username,req.body.usersex,req.body.baday,req.body.mail,req.body.qq,city,req.session.userno,function(result){
        res.json({res:1});
        delete req.session.userID;
    })

});


router.get('/editor-icon',function(req, res) {
    if(req.session.userID==undefined){
        req.session.urldate='/users/editor-icon';
        res.redirect('/login');
    }else {
    res.render('editor-icon',{username:req.session.userID})
    }

});

router.post('/editor-icon',function(req, res) {
    console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');

    var form=new formidable.IncomingForm();

    form.uploadDir="../public/upload/";

    form.parse(req,function(err,fields,files){
        if(err){
            res.locals.error=err;
            return;
        }

        var extName='';//后缀名
        switch(files.fileImage01.type){
            case 'image/jpeg':
                extName='jpeg';
                break;
            case 'image/jpg':
                extName='jpg';
                break;
            case 'image/png':
                extName='png';
                break;
            case 'image/x-png':
                extName='png';
                break;
        }

        if(extName.length==0){
            res.send('只支持png和jpg格式图片');
            return;
        }else{
            form.uploadDir="../public/images/";
            form.keepExtensions=true;
            form.maxFieldsSize=2*1024;
            var avatarName=Math.random()+'.'+extName;
            var newPath=form.uploadDir+avatarName;

            console.log('这个是'+newPath);
            console.log("old"+files.fileImage01.path);

            console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');

            console.log('第一个'+files.fileImage01.path);
            console.log('第二个'+newPath);

            fs.renameSync(files.fileImage01.path,newPath);
            res.locals.success='上传成功....';
            console.log('add img ---end');
        }

        personal.update_icon(avatarName,req.session.usertel,function(result){
            if(result.affectedRows==1){
                res.json({"res":"1"});
            }else{
                res.json({"res":"0"});
            }
        })
    });


});












router.get('/detail/:id',function(req,res){
    req.session.foster=req.params.id;
    console.log('foster:' + req.session.foster);
    var index=parseInt(req.params.id);
    shopDAO.get_shop_details(index,function(result){
        console.log(result[0]);
        res.render('w_bussiness_details', {res:result,username:req.session.userID})
    });
});


router.get('/buy',function(req,res){

    if(req.session.userID==undefined){
        req.session.urldate='/users/buy';
        res.redirect('/login');
    }else {
        petDAO.getinfo(req,res,function(data) {
            console.log(data);
            res.render('w_reservation', {pet: data,username:req.session.userID});
        });
    }
});
router.get('/apply_foster',function(req,res){

    if(req.session.userID==undefined){
        req.session.urldate='/users/apply_foster';
        res.redirect('/login');
    }
    else {
        personal.get_usertype(req.session.userno,function(result){
            console.log('000000000000000'+JSON.stringify(result));
            console.log(result[0].usertype);
            if(result[0].usertype==3){
                res.render('homepage',{username:req.session.userID});

            }else{
                res.render('apply_foster',{username:req.session.userID});

            }
        });

    }
});

router.get('/mypet',function(req,res){
    if(req.session.userID==undefined){
        req.session.urldate='/users/mypet';
        res.redirect('/login');
    }else {
        petDAO.getinfo(req,res,function(data){
            console.log(data);
            res.render('mypet',{pet:data,username:req.session.userID});
        });
    }
});
router.get('/myorder',function(req,res){
    if(req.session.userID==undefined){
        req.session.urldate='/users/myorder';
        res.redirect('/login');
    }else {
        orderDAO.getinfo(req,res,function(data){
            console.log(data);
            res.render('myorder',{order:data,username:req.session.userID});
        });
    }
});

router.get('/apply_foster_message',function(req,res){
    if(req.session.userID==undefined){
        req.session.urldate='/users/apply_foster_message';
        res.redirect('/login');
    }else {
        res.render('foster',{username:req.session.userID});
    }

});

router.get('/collection',function(req,res){
    if(req.session.userID==undefined){
        req.session.urldate='/users/collection';
        res.redirect('/login');
    }else {
        personal.get_article(req.session.userID, function (result) {

            console.log('__________>>>' + JSON.stringify(result));
            res.render('collection', {res: result, username: req.session.userID});
        });
    }
});
router.post('/collection',function(req,res) {
    var artno=parseInt(req.body.artno);
personal.delete_article(artno,function(result){
    if(result.affectedRows>=1){
        res.json({res:1})
    }
})

});




module.exports = router;
